<?php
/**
 * Class that operate on table 'formulariocampo'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2009-11-25 19:00
 */
class FormularioCampoDAO{

    /**
     * Get all records from table
     */
    public function queryAll(){
            $sql = 'SELECT * FROM formulariocampo';
            $sqlQuery = new SqlQuery($sql);
            return $this->getList($sqlQuery);
    }

    /**
     * Get all records from table ordered by field
     *
     * @param $orderColumn column name
     */
    public function queryAllOrderBy($orderColumn){
            $sql = 'SELECT * FROM formulariocampo ORDER BY '.$orderColumn;
            $sqlQuery = new SqlQuery($sql);
            return $this->getList($sqlQuery);
    }

    /**
     * Delete record from table
     * @param formulariocampo primary key
     */
    public function delete($codCampo){
            $sql = 'DELETE FROM formulariocampo WHERE codCampo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($codCampo);
            return $this->executeUpdate($sqlQuery);
    }

    /**
     * Insert record to table
     *
     * @param FormularioCampoMySql formulariocampo
     */
    public function insert($codFormulario, $codCampo){
            $sql = 'INSERT INTO formulariocampo (codFormulario, codCampo) VALUES (?, ?)';
            $sqlQuery = new SqlQuery($sql);

            $sqlQuery->set($codFormulario);
            $sqlQuery->set($codCampo);

            $this->executeInsert($sqlQuery);
    }

    /**
     * Query
     */
    public function queryByCodFormulario($value){
            $sql = 'SELECT * FROM formulariocampo WHERE codFormulario = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

    public function queryByCodCampo($value){
            $sql = 'SELECT * FROM formulariocampo WHERE codCampo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

    public function deleteByCodCampo($value){
            $sql = 'DELETE FROM formulariocampo WHERE codCampo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($value);
            return $this->executeUpdate($sqlQuery);
    }

    public function deleteByCodFormulario($value){
            $sql = 'DELETE FROM formulariocampo WHERE codFormulario = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->executeUpdate($sqlQuery);
    }

    /**
     * Read row
     *
     * @return FormularioCampoMySql
     */
    protected function readRow($row){
            $formulariocampo = new FormularioCampo();

            $formulariocampo->codFormulario = $row['codFormulario'];
            $formulariocampo->codCampo = $row['codCampo'];

            return $formulariocampo;
    }

    protected function getList($sqlQuery){
            $tab = QueryExecutor::execute($sqlQuery);
            $ret = array();
            for($i=0;$i<count($tab);$i++){
                    $ret[$i] = $this->readRow($tab[$i]);
            }
            return $ret;
    }

    /**
     * Get row
     *
     * @return FormularioCampoMySql
     */
    protected function getRow($sqlQuery){
            $tab = QueryExecutor::execute($sqlQuery);
            return $this->readRow($tab[0]);
    }


    /**
     * Execute sql query
     */
    protected function execute($sqlQuery){
            return QueryExecutor::execute($sqlQuery);
    }


    /**
     * Execute sql query
     */
    protected function executeUpdate($sqlQuery){
            return QueryExecutor::executeUpdate($sqlQuery);
    }

    /**
     * Insert row to table
     */
    protected function executeInsert($sqlQuery){
            return QueryExecutor::executeInsert($sqlQuery);
    }

	/**
	 * Metodo que retorna todos os campos de um formulario
	 * @param int codFormulario
	 * @return array codCampo
	 */
	public function listarCamposFormulario($codFormulario) {
		$sql = 'SELECT * FROM formulariocampo WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codFormulario);
		$tab = $this->execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$formulariocampo = new FormularioCampo();
			$formulariocampo->codFormulario = $tab[$i]["codFormulario"];
			$formulariocampo->codCampo = $tab[$i]["codCampo"];
			$ret[$i] = $formulariocampo;
		}
		return $ret;
	}

}
?>
